import java.io.IOException;

import javax.xml.transform.TransformerException;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.ShutdownSignalException;

import com.herohead.*;


public class m_getMail {

	/**
	 * @param args
	 * @throws TransformerException 
	 */

	static Channel ch;


	private static String queue = null;

	static Channel channel;


	public static void main(String[] args) throws TransformerException, IOException, Throwable, ConsumerCancelledException, InterruptedException {
		// TODO Auto-generated method stub

		
		Xml x = new Xml();
		String queue = "email";


		Rabbitmq rmq = new Rabbitmq();

		rmq.setHostname("us01s-netops01");
		rmq.setLogin("backup", "networker", "backup");
		rmq.setupChannel();

		rmq.setQueue(queue);

		rmq.declareQueDurable();
		channel = rmq.getChannel();

		QueueingConsumer consumer = new QueueingConsumer(channel);

		channel.basicConsume(rmq.getQueue(), true, consumer);

		channel = rmq.getChannel();

		System.out.println("Waiting for message");
		while (true) {
			QueueingConsumer.Delivery delivery = consumer.nextDelivery();

			String message = new String(delivery.getBody());
			x.readEmail(message);
			Email em = new Email(x.getSmtp());
			em.addToAddress(x.getToEmail());
			em.addFromAddress(x.getFromEmail());
			em.sendMail(x.getSubject(), x.getBody());
			System.out.println(message);
		}

	}
}


